import java.util.*;

/**
 * @class AnagramsChecker 
 * 
 * This program implements Task 3 from the "Java Basics" chapter of "Java SE" training:
 * It receives input of two phrases and give the output to screen telling whether
 * these words are anagrams.
 *
 * @version 1.0
 * @date 2013-06-18
 * @author Inna Ianko
*/
public class AnagramsChecker {
	
	public static final String PROGRAM_DESCRIPTION = 
			"This program will tell whether entered phrases are anagrams.";	
	public static final String ENTER_DATA_MESSAGE =
			"Please enter phrase";
	public static final String ENTER_DATA_MESSAGE_LONG =
			"Please enter phrase which contains only letters or spaces.";	
	public static final String INPUT_IS_INVALID_MESSAGE = 
			"Yoy have entered invalid phrase."; 				
	public static final String APOSTROPHE = "'";
	public static final String JOIN_FOR_PHRASES = "and";	
	public static final String NEXT_LINE = "\n";
	public static final char EMPTY_SPACE = ' ';
	public static final char COLON = ':';	
	public static final String LINES_SEPARATOR =
			"----------------------------------------------------------------";
	public static final String DATA_ENTERED_HEADER_MESSAGE =
			"You have entered next phrases";
	public static final String NOT_ANAGRAMS_MESSAGE =
			"The words you have entered are NOT anagrams.";
	public static final String ANAGRAMS_MESSAGE = 
			"The words you have entered are anagrams!";		
	public static final int ZERO = 0;
	public static final char[] ALPHABET = 
			{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
			'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};					
	public static final int NUMBER_OF_INPUT_PHRASES = 2;
	
	public static void main (String[] args) {
	
		printDescription(PROGRAM_DESCRIPTION);
		
		String[] phrasesToCheck = new String[NUMBER_OF_INPUT_PHRASES];
		
		phrasesToCheck = getValidInputPhrases();
		printEntredPhrases(phrasesToCheck);		
		
		boolean wordsAreAnagrams = checkIfAnagrams(phrasesToCheck[0],
				phrasesToCheck[1]);	
		
		printResult(wordsAreAnagrams);
	}

	public static String[] getValidInputPhrases() {
						 												   			
		String[] result = new String[NUMBER_OF_INPUT_PHRASES];			
		boolean dataEnteredIsValid;
								
		for (int indexOfPhrase = 0; indexOfPhrase < result.length; indexOfPhrase++) {					
			do {	
				result[indexOfPhrase] = askUserForInputPhrase(indexOfPhrase);								
				dataEnteredIsValid = checkPhraseContainsOnlyLetters(result[indexOfPhrase]);				
				printInvalidInputStateIfTrue(dataEnteredIsValid);
			} while (!dataEnteredIsValid);
		}		
		return result;
	}
	
	public static String askUserForInputPhrase(int indexOfPhrase) {
		
		Scanner input = new Scanner(System.in);
		
		System.out.println(ENTER_DATA_MESSAGE + (indexOfPhrase + 1) + COLON);	
		return input.nextLine();
	}
	
	public static String removeSpacesFromString(String phrase) {
	
		int currentIndexOfChar = 0;
		
		while (currentIndexOfChar < phrase.length()) {				
			if (phrase.charAt(currentIndexOfChar) == EMPTY_SPACE) {			
				if (currentIndexOfChar > ZERO) {
					phrase = phrase.substring(0, currentIndexOfChar) 
						   + phrase.substring(currentIndexOfChar+1);
				} else {
					phrase = phrase.substring(currentIndexOfChar+1);	
				}					
			}
			else {
				++currentIndexOfChar;	
			}				
		}
		return phrase;
	}
		
	public static boolean checkPhraseContainsOnlyLetters(String phrase)	{
	 	 			 
		phrase = removeSpacesFromString(phrase);	
		
		int indexOfCharInPhrase = 0;    
		boolean charIsNotLetter;
		boolean result = true;
				
		while (indexOfCharInPhrase < phrase.length()) {		
			charIsNotLetter = true;
			
			for (int indexOfCharInAlphabet = 0; indexOfCharInAlphabet < ALPHABET.length; indexOfCharInAlphabet++) {			
				if (phrase.charAt(indexOfCharInPhrase) == ALPHABET[indexOfCharInAlphabet]) {
					charIsNotLetter = false;
				}					
			}
			
			if (charIsNotLetter) {			
				result = false;		
				break;				
			} else {
				++indexOfCharInPhrase;	
			}				
		}
		return result;
	}
 	
	public static void printInvalidInputStateIfTrue(boolean dataEnteredIsValid) {
	
		if (!dataEnteredIsValid) {												  		
			System.out.println(NEXT_LINE
					+ INPUT_IS_INVALID_MESSAGE + NEXT_LINE 					
					+ ENTER_DATA_MESSAGE_LONG + NEXT_LINE);
		}									  
	}
	
	public static boolean checkIfAnagrams(String first_phrase, String second_phrase) {	

		first_phrase = removeSpacesFromString(first_phrase.toLowerCase());
		second_phrase = removeSpacesFromString(second_phrase.toLowerCase());
			
		if (first_phrase.length() == second_phrase.length()) {		
		
			first_phrase  = sortCharsInStringAlphabetically(first_phrase);
			second_phrase = sortCharsInStringAlphabetically(second_phrase);
			
			if (second_phrase.equals(first_phrase)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static String sortCharsInStringAlphabetically(String inputPhrase) {
    
		String result = "";
		
		for (int indexOfCharInAlphabet = 0;
				indexOfCharInAlphabet < ALPHABET.length; indexOfCharInAlphabet++) {		
				
			for (int indexOfCharInPhrase = 0; 
					indexOfCharInPhrase < inputPhrase.length(); indexOfCharInPhrase++) {
					
				if (inputPhrase.charAt(indexOfCharInPhrase) 
						== ALPHABET[indexOfCharInAlphabet]) {						
					result = result + inputPhrase.charAt(indexOfCharInPhrase);	
				}					
			}
		}
		
		return result;			
	}
	
	public static void printEntredPhrases(String[] enteredPhrases) {
				
		final int indexOfLastChar = enteredPhrases.length - 1; 		
		String result = NEXT_LINE + DATA_ENTERED_HEADER_MESSAGE + COLON;
		
		for (int indexOfPhrase = 0; indexOfPhrase < enteredPhrases.length; indexOfPhrase++) {		
			result = result + EMPTY_SPACE + APOSTROPHE;
			result = result + enteredPhrases[indexOfPhrase];
            result = result + APOSTROPHE + EMPTY_SPACE;
			
			if (indexOfPhrase != indexOfLastChar) {
				result = result + JOIN_FOR_PHRASES;
			}
        }
		System.out.println(result);	
	}
		
	public static void printResult(boolean wordsAreAnagrams) {
	
		if (wordsAreAnagrams) {
			System.out.println(NEXT_LINE + ANAGRAMS_MESSAGE);
		} else {
			System.out.println(NEXT_LINE + NOT_ANAGRAMS_MESSAGE);
		}
	}
	
	public static void printDescription(String description) {
			
		System.out.println(NEXT_LINE + LINES_SEPARATOR);
		System.out.println(NEXT_LINE + EMPTY_SPACE + description);
		System.out.println(LINES_SEPARATOR);
	}
}